package net.tycmc.cty.ctyms.timejob.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.cty.ctyms.timejob.common.NumberFormat;
import net.tycmc.bulb.common.util.MapGetter;

/**
 * 载重段集合
 * @author haoyang
 *
 */
public class LoadLines {
	
	/**
	 * 载重段集合
	 */
	private List<LoadLine> loadLines = new ArrayList<LoadLine>();
	
	public LoadLines(List<LoadLine> loadLines){
		this.loadLines = loadLines;
	}

	public final List<LoadLine> getLoadLines() {
		return loadLines;
	}
	/**
	 * 获取载重统计结果：空载0，装载：1，满载：2，特满载：3，合计：4
	 * @return 
	 *runKm-行驶公里数
	 *runTime-行驶小时数
	 *oilCons-油耗
	 *oilRate-油耗率
	 *runKmPercent-公里百分比
	 *runTimePercent-小时百分比
	 *TKM-吨公里
	 *U-单耗
	 *totalRunKm-总行驶里程
	 *totalRunTime-总行驶时间
	 *totalOilCons-总油耗
	 */
	public final List<Map<String,Object>> getValueByLoadType(){
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>(5);
		//公里数，小时数，油耗，油耗率，吨公里，单耗，公里百分比，小时百分比
		Map<String,Object> total = new HashMap<String, Object>();
		double totalRunKm = 0;
		double totalRunTime = 0;
		double totalOilCons = 0;
		List<double[]> allValues = getRunKmTimeOil();
		double values[] = null;
		double runKm = 0;
		double runTime = 0;
		double oilCons = 0;
		double oilRate = 0;
		//吨公里
		double TKM = 0;
		//单耗
		double U = 0;
		for(int i=0;i<4;i++){
			Map<String,Object> retMap = new HashMap<String, Object>();
			values = allValues.get(i);
			runKm = values[0];
			runTime = values[1];
			oilCons = values[2];
			oilRate = caculOilRate(NumberFormat.round(oilCons, 2),NumberFormat.round(runKm, 2) );
			retMap.put("runKm", runKm);
			retMap.put("runTime", runTime);
			retMap.put("oilCons", oilCons);
			retMap.put("oilRate", oilRate);
			if(runKm>0)
				totalRunKm +=runKm;
			if(runTime>0)
				totalRunTime +=runTime;
			if(oilCons>0)
				totalOilCons +=oilCons;
			//吨公里
			TKM = values[4];
			//单耗
			U = caculUCons(oilCons, TKM);
			retMap.put("TKM", TKM);
			retMap.put("U", U);
			retList.add(retMap);
			retMap=null;
		}
		total.put("totalRunKm",totalRunKm );
		total.put("totalRunTime", totalRunTime);
		total.put("totalOilCons", totalOilCons);
		for (Map<String,Object> map : retList) {
			map.put("runKmPercent", caculKmPercent(MapGetter.getDouble(map, "runKm"), MapGetter.getDouble(total, "totalRunKm")));
			map.put("runTimePercent", caculKmPercent(MapGetter.getDouble(map, "runTime"), MapGetter.getDouble(total, "totalRunTime")));
		}
		retList.add(total);
		//无用对象 置空
		total=null;
		allValues = null;
		return retList;
	}
	/**
	 * 获取 行驶公里数,行驶小时数,总油耗
	 * @return
	 */
	private List<double[]> getRunKmTimeOil(){
		List<double[]> retList = new ArrayList<double[]>(5);
		double retValue0[] = new double[6];
		double retValue1[] = new double[6];
		double retValue2[] = new double[6];
		double retValue3[] = new double[6];
		double retValue4[] = new double[6];
		for (LoadLine currll : loadLines) {
			//合计数据	
			retValue0[0] += currll.getValueByParamName("runKm");
			retValue0[1] += currll.getValueByParamName("runTime");
			retValue0[2] += currll.getValueByParamName("oilCons");
			retValue0[3] += currll.getValueByParamName("load");
			retValue0[4] += currll.getValueByParamName("TKM");
			retValue0[5] += currll.getValueByParamName("U");
			if("1".equals(currll.getLoadType())){//计算空载相应参数 值
				retValue1[0] += currll.getValueByParamName("runKm");
				retValue1[1] += currll.getValueByParamName("runTime");
				retValue1[2] += currll.getValueByParamName("oilCons");
				retValue1[3] += currll.getValueByParamName("load");
				retValue1[4] += currll.getValueByParamName("TKM");
				retValue1[5] += currll.getValueByParamName("U");
			}else if("2".equals(currll.getLoadType())){//计算装载相应参数 值
				retValue2[0] += currll.getValueByParamName("runKm");
				retValue2[1] += currll.getValueByParamName("runTime");
				retValue2[2] += currll.getValueByParamName("oilCons");
				retValue2[3] += currll.getValueByParamName("load");
				retValue2[4] += currll.getValueByParamName("TKM");
				retValue2[5] += currll.getValueByParamName("U");
			}else if("3".equals(currll.getLoadType())){//计算满载相应参数 值
				retValue3[0] += currll.getValueByParamName("runKm");
				retValue3[1] += currll.getValueByParamName("runTime");
				retValue3[2] += currll.getValueByParamName("oilCons");
				retValue3[3] += currll.getValueByParamName("load");
				retValue3[4] += currll.getValueByParamName("TKM");
				retValue3[5] += currll.getValueByParamName("U");
			}else if("4".equals(currll.getLoadType())){//计算特满载相应参数 值
				retValue4[0] += currll.getValueByParamName("runKm");
				retValue4[1] += currll.getValueByParamName("runTime");
				retValue4[2] += currll.getValueByParamName("oilCons");
				retValue4[3] += currll.getValueByParamName("load");
				retValue4[4] += currll.getValueByParamName("TKM");
				retValue4[5] += currll.getValueByParamName("U");
			}
		}
		retList.add(retValue1);
		retList.add(retValue2);
		retList.add(retValue3);
		retList.add(retValue4);
		retList.add(retValue0);
		retValue1=null;
		retValue2=null;
		retValue3=null;
		retValue4=null;
		retValue0=null;
		return retList;
	}
	/**
	 * 计算 油耗率
	 * @param 
	 * @return
	 */
	private double caculOilRate(double totalOil,double runKm){
		if(totalOil>=0&&runKm>0)
			return totalOil/runKm*100;
		return -1;
	}
	/**
	 * 计算 吨公里 :TKM = runKm*load/1000
	 * @param 
	 * @return
	 */
	private double caculTKm(double load,double runKm){
		if(runKm>=0&&load>=0)
			return runKm*load/1000;
		return -1;
	}
	/**
	 * 计算 单耗 :U = OilCons/TKM
	 * @param 
	 * @return
	 */
	private double caculUCons(double oilCons,double TKM){
		if(oilCons>=0&&TKM>0)
			return oilCons/TKM;
		return -1;
	}
	/**
	 * 计算 公里百分比	
	 * @param 
	 * @return
	 */
	private double caculKmPercent(double runKm,double totalRunKm){
		if(runKm>=0&&totalRunKm>0)
			return runKm/totalRunKm*100;
		return -1;
	}
	/**
	 * 计算 小时百分比
	 * @param 
	 * @return
	 */
	private double caculTimePercent(double runTime,double totalRunTime){
		if(runTime>=0&&totalRunTime>0)
			return runTime/totalRunTime*100;
		return -1;
	}
}